package com.lingkang.xcoa.config;

import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * @author linke
 * @date 2020/8/29 18:52
 * @description
 */
@Configuration
@Slf4j
public class DbConfig {

  @Primary
  @Bean(name = "flowableDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.flowable.hikari")
  public DataSource flowableDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "mybatisDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.mybatis.hikari")
  public DataSource mybatisDataSource() {
    return DataSourceBuilder.create().build();
  }

  /**
   * mybatis 事务支持
   */
  @Bean(name = "mybatisTransactionManager")
  public DataSourceTransactionManager mybatisTransactionManager(
      @Qualifier("mybatisDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  /**
   * flowable 事务支持，默认主数据源事务
   */
  @Bean(name = "transactionManager")
  @Primary
  public DataSourceTransactionManager transactionManager(
      @Qualifier("flowableDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  /**
   * 初始化流程引擎
   *
   * @return
   */
  @Bean(name = "processEngine")
  public ProcessEngine initProcessEngine(@Qualifier("flowableDataSource") DataSource dataSource) {
    log.info("=============================ProcessEngineBegin=============================");
    // 流程引擎配置
    ProcessEngineConfiguration cfg = null;
    try {
      cfg = new StandaloneProcessEngineConfiguration()
          .setDataSource(dataSource)
          // 初始化基础表，不需要的可以改为 DB_SCHEMA_UPDATE_FALSE
          .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)//不更新
          // 解决流程图乱码
          .setActivityFontName("宋体")
          .setLabelFontName("宋体")
          .setAnnotationFontName("宋体");
    } catch (Exception e) {
      e.printStackTrace();
    }
    // 初始化流程引擎对象
    ProcessEngine processEngine = cfg.buildProcessEngine();
    log.info("=============================ProcessEngineEnd=============================");
    return processEngine;
  }

  @Bean
  public SqlSessionFactory mybatisSqlSessionFactory() throws Exception {
    log.info("sqlSessionFactoryCRM 创建成功!---------------------------------------------------");
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();//创建sqlSessionFactory
    factoryBean.setDataSource(mybatisDataSource());
    //配置文件中的xml扫描无效，此处优先级高
    factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources("classpath:mapper/*.xml"));//手动配置读取映射文件的路径
    return factoryBean.getObject();
  }

  @Bean
  public SqlSessionTemplate mybatisSqlSessionTemplate() throws Exception {
    SqlSessionTemplate template = new SqlSessionTemplate(
        mybatisSqlSessionFactory()); // 使用上面配置的Factory
    return template;
  }
}
